package com.sk.sourcecircle.activity.find.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jude.rollviewpager.hintview.IconHintView;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.activity.find.act.MoreSourceActivity;
import com.sk.sourcecircle.activity.find.act.SatisfyTaskDetailsActvitiy;
import com.sk.sourcecircle.activity.find.act.TaskDetailsActivity;
import com.sk.sourcecircle.activity.home.activity.EventDetailsActivity;
import com.sk.sourcecircle.adapter.BannerAdapter;
import com.sk.sourcecircle.adapter.RoundEventAdapter;
import com.sk.sourcecircle.adapter.RoundSatisfyTaskAdapter;
import com.sk.sourcecircle.adapter.RoundTaskAdapter;
import com.sk.sourcecircle.app.MyApplication;
import com.sk.sourcecircle.base.BaseFragment;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.callback.MyCallBack;
import com.sk.sourcecircle.entity.HomeBannerBean;
import com.sk.sourcecircle.entity.RoundSourceBean;
import com.sk.sourcecircle.utils.AppApi;
import com.sk.sourcecircle.utils.SnackUtils;
import com.sk.sourcecircle.utils.UIUtils;
import com.sk.sourcecircle.view.EasyLoadingMoreView;
import com.sk.sourcecircle.view.FixedProportionViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by sk on 2017/11/28.
 */

public class RoundSourceFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, HandleDataCallBack {


    @BindView(R.id.my_pager)
    public FixedProportionViewPager myPager;
    @BindView(R.id.line2)
    View line2;
    @BindView(R.id.more_event)
    RelativeLayout moreEvent;
    @BindView(R.id.event_ryc)
    RecyclerView eventRyc;
    @BindView(R.id.line3)
    View line3;
    @BindView(R.id.more_task)
    RelativeLayout moreTask;
    @BindView(R.id.task_ryc)
    RecyclerView taskRyc;
    @BindView(R.id.line4)
    View line4;
    @BindView(R.id.more_sys_ryc)
    RelativeLayout moreSysRyc;
    @BindView(R.id.share_ryc)
    RecyclerView shareRyc;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    Unbinder unbinder;
    @BindView(R.id.swipe)
    SwipeRefreshLayout swipe;
    Unbinder unbinder1;
    private int page = 1;

    private int cateId = 0;

    RoundEventAdapter eventAdapter;
    RoundTaskAdapter taskAdapter;
    RoundSatisfyTaskAdapter satisfyTaskAdapter;

    private List<RoundSourceBean.DataBean.ActivityBean> activityBeanList = new ArrayList<>();
    private List<RoundSourceBean.DataBean.Mission0Bean> mission0BeanList = new ArrayList<>();
    private List<RoundSourceBean.DataBean.Mission1Bean> mission1BeanList = new ArrayList<>();

    private MyCallBack myCallBack;
    private boolean isRefresh = false;

    public MyCallBack getMyCallBack() {
        return myCallBack;
    }

    public void setMyCallBack(MyCallBack myCallBack) {
        this.myCallBack = myCallBack;
    }

    public FixedProportionViewPager getMyPager() {
        return myPager;
    }

    public void setMyPager(FixedProportionViewPager myPager) {
        this.myPager = myPager;
    }

    @Override
    protected int getContentView() {
        return R.layout.round_source;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        swipe.setColorSchemeResources(R.color.home_checked_color, R.color.home_checked_color);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                doRefresh();
            }
        });

        eventRyc.setLayoutManager(new LinearLayoutManager(mContext));
        eventAdapter = new RoundEventAdapter(R.layout.item_round_source, activityBeanList);
        eventRyc.setAdapter(eventAdapter);
        EasyLoadingMoreView loadMoreView = new EasyLoadingMoreView();
        eventAdapter.setLoadMoreView(loadMoreView);
        View emptyView = getLayoutInflater().inflate(R.layout.empty_view, null);
        eventAdapter.setEmptyView(emptyView);
        eventAdapter.setOnItemClickListener(this);

        myPager.setHintView(new IconHintView(mContext, R.drawable.point_focus, R.drawable.point_normal, UIUtils.dip2px(mContext, 16)));


        taskRyc.setLayoutManager(new LinearLayoutManager(mContext));
        taskAdapter = new RoundTaskAdapter(R.layout.item_recommend_event, mission0BeanList);
        taskRyc.setAdapter(taskAdapter);
        EasyLoadingMoreView loadMoreView1 = new EasyLoadingMoreView();
        taskAdapter.setLoadMoreView(loadMoreView1);
        View emptyView1 = getLayoutInflater().inflate(R.layout.empty_view, null);
        taskAdapter.setEmptyView(emptyView1);
        taskAdapter.setOnItemClickListener(this);

        shareRyc.setLayoutManager(new LinearLayoutManager(mContext));
        satisfyTaskAdapter = new RoundSatisfyTaskAdapter(R.layout.item_recommend_event, mission1BeanList);
        shareRyc.setAdapter(satisfyTaskAdapter);
        EasyLoadingMoreView loadMoreView2 = new EasyLoadingMoreView();
        satisfyTaskAdapter.setLoadMoreView(loadMoreView2);
        View emptyView2 = getLayoutInflater().inflate(R.layout.empty_view, null);
        satisfyTaskAdapter.setEmptyView(emptyView2);
        satisfyTaskAdapter.setOnItemClickListener(this);


        eventRyc.setHasFixedSize(true);
        eventRyc.setNestedScrollingEnabled(false);

        taskRyc.setHasFixedSize(true);
        taskRyc.setNestedScrollingEnabled(false);

        shareRyc.setHasFixedSize(true);
        shareRyc.setNestedScrollingEnabled(false);

        AppApi.getRoundBanner(MyApplication.getInstance().getLat(), MyApplication.getInstance().getLng(), this, 1, getActivity());
        AppApi.roundSource(MyApplication.getInstance().getLat(), MyApplication.getInstance().getLng(), this, 0, getActivity());
    }

    public void doRefresh() {
        swipe.setRefreshing(true);
        isRefresh = true;
        AppApi.getRoundBanner(MyApplication.getInstance().getLat(), MyApplication.getInstance().getLng(), this, 1, getActivity());
        AppApi.roundSource(MyApplication.getInstance().getLat(), MyApplication.getInstance().getLng(), this, 0, getActivity());
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

        if (adapter instanceof RoundEventAdapter) {
            Intent intent = new Intent(mContext, EventDetailsActivity.class);
            intent.putExtra("id", activityBeanList.get(position).getActivity_id());
            startActivity(intent);
        } else if (adapter instanceof RoundTaskAdapter) {
            Intent intent = new Intent(mContext, TaskDetailsActivity.class);
            intent.putExtra("id", mission0BeanList.get(position).getActivity_id());
            startActivity(intent);
        } else if (adapter instanceof RoundSatisfyTaskAdapter) {
            Intent intent = new Intent(mContext, SatisfyTaskDetailsActvitiy.class);
            intent.putExtra("id", mission1BeanList.get(position).getActivity_id());
            startActivity(intent);
        }
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        switch (tag) {
            case 0:

                if (isRefresh) {
                    swipe.setRefreshing(false);
                    isRefresh = false;
                    SnackUtils.show(mRootView,"刷新成功！");
                }

                RoundSourceBean roundSourceBean = JSON.parseObject(result, RoundSourceBean.class);
                List<RoundSourceBean.DataBean.ActivityBean> activityBeans = roundSourceBean.getData().getActivity();
                List<RoundSourceBean.DataBean.Mission0Bean> missionBeans = roundSourceBean.getData().getMission_0();
                List<RoundSourceBean.DataBean.Mission1Bean> staifyMissionBeans = roundSourceBean.getData().getMission_1();

                activityBeanList.clear();
                mission0BeanList.clear();
                mission1BeanList.clear();

                activityBeanList.addAll(activityBeans);
                mission0BeanList.addAll(missionBeans);
                mission1BeanList.addAll(staifyMissionBeans);

                eventAdapter.notifyDataSetChanged();
                taskAdapter.notifyDataSetChanged();
                satisfyTaskAdapter.notifyDataSetChanged();

                break;
            case 1:
                HomeBannerBean homeBannerBean = JSON.parseObject(result, HomeBannerBean.class);
                List<HomeBannerBean.DataBean> bannerList = homeBannerBean.getData();
                BannerAdapter bannerAdapter = new BannerAdapter(bannerList, mContext);
                myPager.setAdapter(bannerAdapter);
                break;
        }

    }

    @Override
    public void handlerFailData(String failString, int tag) {
    }

    @Override
    public void handleErrorData(String error, int tag) {

    }

    @OnClick({R.id.more_event, R.id.more_task, R.id.more_sys_ryc})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.more_event:
                Intent eventIntent = new Intent(mContext, MoreSourceActivity.class);
                eventIntent.putExtra("type", 0);
                startActivity(eventIntent);
                break;
            case R.id.more_task:
                Intent taskIntent = new Intent(mContext, MoreSourceActivity.class);
                taskIntent.putExtra("type", 1);
                startActivity(taskIntent);
                break;
            case R.id.more_sys_ryc:
                Intent sycIntent = new Intent(mContext, MoreSourceActivity.class);
                sycIntent.putExtra("type", 2);
                startActivity(sycIntent);
                break;
        }
    }
}
